package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.myutils.ImageAdapterCall;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * Created by 嗨哟 on 2017/3/15.
 */
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageHolder> {
    private Context context;
    private String[]str;
    private LayoutInflater layoutInflater;
    private ImageAdapterCall imageAdapterCall;

    public ImageAdapter(Context context, String[]str) {
        this.context = context;
        this.str = str;
        layoutInflater=LayoutInflater.from(context);
    }

    public void UpData(String[]str){
        this.str = str;
        this.notifyDataSetChanged();
    }

    public void setImageAdapterCall(ImageAdapterCall imageAdapterCall){
        this.imageAdapterCall=imageAdapterCall;
    }

    @Override
    public ImageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=layoutInflater.inflate(R.layout.imageitem,parent,false);
        AutoUtils.autoSize(view);
        return new ImageHolder(view);
    }

    @Override
    public void onBindViewHolder(ImageHolder holder, int position) {
        holder.position=position;
        if(str!=null&&str.length!=0){
            Glide.with(context)
                    .load(new GlideUrl(Constants.DOWN_USER_PICTURE+str[position],new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID",MyInfo.User_Id+"").build()))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.imageitem);
        }else{
            holder.imageitem.setImageResource(R.mipmap.noteimage);
        }

    }

    @Override
    public int getItemCount() {
        return str==null?5:str.length;
    }

    class ImageHolder extends RecyclerView.ViewHolder{
        int position;
        ImageView imageitem;
        public ImageHolder(View itemView) {
            super(itemView);
            imageitem= (ImageView) itemView.findViewById(R.id.imageitem);
            imageitem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    imageAdapterCall.onClick(v,position);
                }
            });
        }
    }
}
